from django.db import models
from shortuuidfield import ShortUUIDField

from apps.authuser.models import UserInfo


class Topic(models.Model):
    title = models.CharField(verbose_name="标题",
                             max_length=20)
    desc = models.CharField(verbose_name="描述",
                            max_length=20)
    thumbnail = models.CharField(verbose_name="封面",
                                 max_length=100)
    date_join = models.DateTimeField(verbose_name="创建时间",
                                     auto_now_add=True)

    author = models.ForeignKey(UserInfo,
                               verbose_name="作者",
                               on_delete=models.CASCADE,
                               related_name="topics")

    class Meta:
        db_table = 'el_topic'
        verbose_name = "话题"
        verbose_name_plural = verbose_name
        default_permissions = ()
        permissions = (
            ("view_topic", "查看话题"),
            ("create_topic", "创建话题"),
            ("update_topic", "更新话题"),
            ("delete_topic", "删除话题"),
        )


class Block(models.Model):
    tid = models.ForeignKey(Topic,
                            verbose_name="话题",
                            on_delete=models.CASCADE)
    name = models.CharField(verbose_name="版块名",
                            max_length=20)

    class Meta:
        db_table = 'el_block'
        verbose_name = "话题板块"
        verbose_name_plural = verbose_name
        default_permissions = ()
        permissions = (
            ("view_topic_block", "查看话题板块"),
            ("create_topic_block", "创建话题板块"),
            ("update_topic_block", "更新话题板块"),
            ("delete_topic_block", "删除话题板块"),
        )


class Post(models.Model):
    pid = ShortUUIDField(primary_key=True)
    content = models.TextField(verbose_name="内容")
    author = models.ForeignKey(UserInfo,
                               verbose_name="作者",
                               on_delete=models.DO_NOTHING,
                               related_name="posts")
    up = models.IntegerField(verbose_name="顶",
                             default=0)
    down = models.IntegerField(verbose_name="踩",
                               default=0)
    date_join = models.DateTimeField(verbose_name="创建时间",
                                     auto_now_add=True)

    topic = models.ForeignKey(Topic,
                              verbose_name="话题",
                              on_delete=models.DO_NOTHING,
                              null=True, blank=True,
                              related_name="posts")
    block = models.ForeignKey(Block,
                              verbose_name="话题板块",
                              on_delete=models.DO_NOTHING,
                              null=True, blank=True,
                              related_name="posts")

    class Meta:
        db_table = 'el_post'
        verbose_name = "帖子"
        verbose_name_plural = verbose_name
        default_permissions = ()
        permissions = (
            ("view_post", "查看帖子"),
            ("create_post", "创建帖子"),
            ("update_post", "更新帖子"),
            ("delete_post", "删除帖子"),
        )


class Comment(models.Model):
    content = models.TextField(verbose_name="内容")
    author = models.ForeignKey(UserInfo,
                               verbose_name="作者",
                               on_delete=models.DO_NOTHING,
                               related_name="comments")
    up = models.IntegerField(verbose_name="顶",
                             default=0)
    down = models.IntegerField(verbose_name="踩",
                               default=0)
    date_join = models.DateTimeField(verbose_name="创建时间",
                                     auto_now_add=True)

    class Meta:
        db_table = 'el_comment'
        verbose_name = "评论"
        verbose_name_plural = verbose_name
        default_permissions = ()
        permissions = (
            ("view_comment", "查看评论"),
            ("create_comment", "创建评论"),
            ("update_comment", "更新评论"),
            ("delete_comment", "删除评论"),
        )


class Message(models.Model):
    content = models.TextField(verbose_name="内容")
    sender = models.ForeignKey(UserInfo,
                               verbose_name="发送者",
                               on_delete=models.DO_NOTHING,
                               related_name="messages")
    receiver = models.ForeignKey(UserInfo,
                                 verbose_name="接受者",
                                 on_delete=models.DO_NOTHING)
    date_join = models.DateTimeField(verbose_name="创建时间",
                                     auto_now_add=True)

    is_read = models.BooleanField(verbose_name="是否已读",
                                  default=False)

    class Meta:
        db_table = 'el_message'
        verbose_name = "消息"
        verbose_name_plural = verbose_name
        default_permissions = ()
        permissions = (
            ("view_message", "查看消息"),
            ("create_message", "创建消息"),
            ("update_message", "更新消息"),
            ("delete_message", "删除消息"),
        )
